package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.base.BasePageAdapter;
import com.wuhan.template.module.home.entity.ActivityInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/10 0010.
 */
public class HuoDongPageAdapter extends BasePageAdapter{

    private Context context;
    private List<ActivityInfoEntity.SlideBean> slideBeans;
    private DisplayImageOptions options;
    private ImageLoader loader;
    private final List<ImageView> imageViews;

    public HuoDongPageAdapter(Context context, List<ActivityInfoEntity.SlideBean> slideBeans) {
        this.context = context;
        this.slideBeans = slideBeans;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageOnLoading(R.mipmap.app_logo)
                .build();

        loader = ((BaseApplication)(context.getApplicationContext())).getImageLoader();
        imageViews = new ArrayList<>();
        for (int i = 0; i < slideBeans.size(); i++) {
            imageViews.add(new ImageView(context));
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        position %= slideBeans.size();
        if (position < 0) {
            position = slideBeans.size() + position;
        }
        ActivityInfoEntity.SlideBean slideBean = slideBeans.get(position);
        String imageUrl =slideBean.getIndexpic().getHost()+slideBean.getIndexpic().getDir()+
                slideBean.getIndexpic().getFilepath()+slideBean.getIndexpic().getFilename();
        ImageView currentImg = imageViews.get(position);
        currentImg.setScaleType(ImageView.ScaleType.FIT_XY);
        ((BaseApplication) context.getApplicationContext())
                .getImageLoader()
                .displayImage(imageUrl, currentImg, options);
        ViewParent vp = currentImg.getParent();
        if (vp != null) {
            ViewGroup parent = (ViewGroup) vp;
            parent.removeView(currentImg);
        }
        container.addView(currentImg);

        return currentImg;

    }



    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}
